<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <span>优集二手车</span>
      </div>
      <!-- 登陆表单区域
	          ref代表当前表单引用对象
	         :model 是表格中封装的对象
	    -->
      <el-form ref="loginFormRef" label-width="0" class="login_form" :model="loginForm" :rules="rules"
        @submit.native.prevent>
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont iconuser" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont iconsuo" type="password" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" size="small" @click="login" native-type="submit">登录</el-button>
          <el-button type="warning" size="small" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>

    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        loginForm: {
          username: '',
          password: ''
        },
        rules: {
          username: [
            // required 是否为必填项 message 提示信息 trigger 触发条件
            {
              required: true,
              message: '请输入用户名',
              trigger: 'blur'
            },
            {
              min: 3,
              max: 30,
              message: '长度在 3 到 30 个字符',
              trigger: 'blur'
            }
          ],
          password: [{
              min: 3,
              max: 18,
              message: '长度在 3 到 18 个字符',
              trigger: 'blur'
            },
            {
              required: true,
              message: '请输入密码',
              trigger: 'blur'
            }

          ]
        }
      }
    },
    methods: {
      login() {
        this.$refs.loginFormRef.validate(async valid => {
          if (!valid) return
          const {
            data: res
          } = await this.$http.post('/user/login', this.loginForm)
          if (res.status !== 200) return this.$message.error('用户名或密码错误')
          this.$message.success('登录成功!')
          window.sessionStorage.setItem('token', res.data)

          this.$router.push('/home')
        })
      },
      reset() {
        this.$refs.loginFormRef.resetFields()
      }
    }
  }
</script>

<style lang="less" scoped>
  .login_container {
    background-color: #42B983;
    height: 100%;
  }

  .login_box {
    width: 450px;
    height: 250px;
    background-color: #FFFFFF;
    /* 设定圆弧角*/
    border-radius: 10px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .avatar_box {
      height: 40px;
      width: 100px;
      padding: 10px;
      background-color: #FFFFFF;
      margin: 0 auto;

    }

    .avatar_box span{
      font-size: 20px;
      color:#834324;
      text-align: center;
      display: block;
    }

   .btns {
      display: flex;
      justify-content: flex-end;

    }

    .login_form {
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;
    }

  }
</style>
